package com.john.market.classes;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.john.market.R;

/**
 * Adapter for ListView
 *
 */
public class ProductAdapter extends ArrayAdapter<Product> {
	// Listener for request button
	private OnClickListener mOnClickListener = null;
	
	// List of products
	private ArrayList<Product> mItems = null;
	
	// List of disabled button tags  
	private List<UUID> mDisabled = null;
	
	// Indicates when the button can be disabled if clicked
	private boolean mCanChange = false;
	
	// Default button text
	private String mButtonText = "OK";
	
	/**
	 * Creates new product adapter
	 * 
	 * @param context
	 * Context
	 * 
	 * @param viewResourceId
	 * Resource id of the view
	 * 
	 * @param items
	 * List of products
	 * 
	 * @param buttonText
	 * Button text
	 * 
	 * @param canChangeButton
	 * Indicates when the button can be disabled if clicked
	 * 
	 */
	public ProductAdapter(Context context, int viewResourceId, ArrayList<Product> items,
			String buttonText, boolean canChangeButton) {
		super(context, viewResourceId, items);
		
		// Setting values
		this.mDisabled = new ArrayList<UUID>();
		this.mCanChange = canChangeButton;
		this.mButtonText = buttonText;
		this.mItems = items; 
	}
	
	/**
	 * Sets {@link OnClickListener} for the button
	 * 
	 * @param listener
	 * Listener for the button 
	 * 
	 */
	public void setOnClickListener(OnClickListener listener) {
		this.mOnClickListener = listener;
	}
	
	/**
	 * Enables button with specified tag
	 * 
	 * @param id
	 * Tag of the button
	 * 
	 */
	public void enableButton(UUID id) {
		if (this.mCanChange) {
			// Remove tag from the list of disabled button tags
			this.mDisabled.remove(id);
		}
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		
		// Creating new view if necessary
		if (view == null) {
            LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.product, null);
        } 
				        
		// Get product from the list
		Product product = this.mItems.get(position);
        if (product != null) {
        	// Setting product title
        	TextView title = (TextView)view.findViewById(R.id.title);
			title.setText(product.getTitle());
			
			// Setting product type 
			TextView type = (TextView)view.findViewById(R.id.type);
			type.setText(product.getType().toString());
			
			// Setting product amout
			TextView amount = (TextView)view.findViewById(R.id.amount);
			amount.setText("" + product.getAmount());
			
			// Setting product price
			TextView price = (TextView)view.findViewById(R.id.price);
			price.setText("$" + product.getPrice());
			
			// Setting the button
			Button request = (Button)view.findViewById(R.id.request);
			request.setText(this.mButtonText);
			request.setTag(product.getId());
			
			// Setting listener for the button
			request.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					// Disable button if necessary
					if (mCanChange) {
						UUID id = (UUID)view.getTag();
						((Button)view).setEnabled(false);
						mDisabled.add(id);
					}
					
					// Call outer listener if any
					if (mOnClickListener != null) {
						mOnClickListener.onClick(view);
					}
				}
			});
			
			// Enable or disable the button if necessary
			if (this.mCanChange) {
				boolean enabled = !this.mDisabled.contains(product.getId()); 
				request.setEnabled(enabled);
			}
        }
        
        return view;
	}	
	
}
